<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2 class="login-title">用户注册</h2>
      <el-form :model="registerForm" :rules="registerRules" ref="registerForm" label-position="top">
        <!-- 用户名 -->
        <el-form-item label="用户名" prop="username">
          <el-input
              v-model="registerForm.username"
              prefix-icon="el-icon-user"
              placeholder="请输入用户名"
          ></el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item label="密码" prop="password">
          <el-input
              v-model="registerForm.password"
              type="password"
              prefix-icon="el-icon-lock"
              placeholder="请输入密码"
              show-password
          ></el-input>
        </el-form-item>

        <!-- 确认密码 -->
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
              v-model="registerForm.confirmPassword"
              type="password"
              prefix-icon="el-icon-lock"
              placeholder="请再次输入密码"
              show-password
          ></el-input>
        </el-form-item>

        <!-- 注册按钮 -->
        <div class="button-container">
          <el-form-item>
            <el-button type="primary" @click="handleRegister" :loading="loading">注册</el-button>
            <el-button @click="goToLogin">返回登录</el-button>
          </el-form-item>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus';
import { apiClient } from '@/api/apiClient'; // 调整路径为实际路径

export default {
  data() {
    // 自定义密码确认验证规则
    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== this.registerForm.password) {
        callback(new Error('两次输入的密码不一致'));
      } else {
        callback();
      }
    };

    return {
      registerForm: {
        username: '',
        password: '',
        confirmPassword: ''
      },
      registerRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度至少为6位', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: validateConfirmPassword, trigger: 'blur' }
        ]
      },
      loading: false
    };
  },
  methods: {
    // 注册处理方法
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;

          // 调用注册接口
          apiClient.post('/doctor/register', {
            username: this.registerForm.username,
            password: this.registerForm.password
          })
              .then(response => {
                this.loading = false;
                if (response.data.code === 1) {
                  ElMessage.success('注册成功');
                  this.$router.push('/'); // 注册成功后跳转到登录页
                } else {
                  ElMessage.error(response.data.msg || '注册失败');
                }
              })
              .catch(error => {
                this.loading = false;
                ElMessage.error('注册失败，请稍后重试');
                console.error('注册失败:', error);
              });
        }
      });
    },

    // 返回登录页
    goToLogin() {
      this.$router.push('/');
    }
  }
};
</script>

<style scoped>
/* 复用登录页的样式 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
}

.login-card {
  width: 400px;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
  font-size: 24px;
  color: #333;
}

.button-container {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>